package org.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.DTO.StudentDTO;
import org.example.common.Result;
import org.example.entity.Audit;
import org.example.entity.Choose;
import org.example.entity.User;
import org.example.mapper.ChooseMapper;
import org.example.mapper.UserMapper;
import org.springframework.expression.spel.ast.Selection;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChooseService extends ServiceImpl<ChooseMapper, Choose> {
    @Resource
    UserMapper userMapper;
    @Resource
    ChooseMapper chooseMapper;

    public List<StudentDTO> getStudentsByTeacherId(int teacherId) {
        List<Choose> chooses = chooseMapper.findByTeacherId(String.valueOf(teacherId));
        List<StudentDTO> students = new ArrayList<>();
        for (Choose choose : chooses) {
            User student = userMapper.findById((choose.getStudentId()));
            if (student != null) {
                students.add(new StudentDTO(
                        student.getId(),
                        student.getUsername(),
                        student.getName(),
                        student.getPhone(),
                        student.getEmail(),
                        student.getAvatar(),
                        student.getRole()
                ));
            }
        }
        return students;
    }

    public Result getAllTeachers() {
        QueryWrapper<User> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("role","教师");
        List<User> teachers =userMapper.selectList(queryWrapper);
        return Result.success(teachers);
    }

    public void addSelection(Integer teacherId, Integer userId) {
        Choose existingSelection = chooseMapper.selectOne(new QueryWrapper<Choose>().eq("student_id", userId));
        //查询是否已经选择了教师
        if (existingSelection != null) {
            // 学生已经选择了教师，更新教师ID
            User user = userMapper.selectById(teacherId);
            existingSelection.setTeacherId(teacherId);
            existingSelection.setStatus(("1"));
            existingSelection.setTeacherName(user.getName());
            chooseMapper.updateById(existingSelection);
        } else {
            Choose choose = new Choose();
            User user = userMapper.selectById(teacherId);
            choose.setTeacherName(user.getName());
            choose.setTeacherId(teacherId);
            choose.setStudentId(userId);
            choose.setStatus("1");
            chooseMapper.insert(choose);
        }

    }


    public Result getSelectedTeacher(Integer userId) {
        QueryWrapper<Choose> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("student_id",userId);
        List<Choose> chooseList = chooseMapper.selectList(queryWrapper);
        if (!chooseList.isEmpty()) {
            return Result.success(chooseList);
        } else {
            return Result.error("未找到对应数据");
        }
    }

    public List<String> getStudentIds(Integer teacherId) {
        // 使用 chooseMapper 或其他方式查询符合条件的数据
        List<Choose> chooseList = chooseMapper.selectList(new QueryWrapper<Choose>().eq("teacher_id", teacherId));
        List<String> studentIds = new ArrayList<>();
        // 遍历查询结果，提取学生 ID
        for (Choose choose : chooseList) {
            Integer studentIdInteger = choose.getStudentId(); // 假设 Choose 类中有一个方法用于获取学生 ID
            String studentId = String.valueOf(studentIdInteger); // 将学生 ID 转换为字符串类型
            studentIds.add(studentId);
        }

        return studentIds;
    }

    public boolean delStudent(int id, int teacherId) {
        return chooseMapper.delStudent(id,teacherId);
    }
}
